package MysqlLogin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelectFromMySQL {
    User user= null;
    private String MySQLSelect = "";
    public User login(String username,String password){
        MySQLSelect = "select * from login where username=? and password=?";
        Connection connection = null;
        PreparedStatement ps = null;
        try{
            connection = ConnectionMySQL.getConnection();
            ps = (PreparedStatement) connection.prepareStatement(MySQLSelect);
            //这里相当于将用户名和密码填到SQL语句的问号处
            ps.setString(1,username);
            ps.setString(2,password);
            //得到数据库的查询结果，一个数据集
            ResultSet rs = (ResultSet)ps.executeQuery();
            //判断结果集是否有效
            if(rs.next()){
                user = new User();
                user.setName(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
            connection.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionMySQL.closeConnection();
        }
        return user;
    }
}
